import type { Options, PluginWithOptions } from 'markdown-it'
import type { Renderer, Token } from 'markdown-it/index.js'

export interface APluginOptions { }

export const aPlugin: PluginWithOptions<APluginOptions> = (
    md,
    _options = {}
): void => {
    md.renderer.rules.link_open = (
        tokens: Token[],
        idx: number,
        options: Options,
        _env: any,
        slf: Renderer
    ) => {
        const aIndex = tokens[idx].attrIndex('href')
        if (aIndex !== -1) {
            const t: any = tokens[idx]
            tokens[idx] = t.attrs.push(['target', '_blank'])
        }
        return slf.renderToken(tokens, idx, options)
    }
}
